<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>


<html>
<head>
    <title>添加品牌信息</title>
    <%@ include file="../../common/base.jsp" %>

    <script type="text/javascript">

        //渠道编码
        function brandIdBlur(focusFlag) {
            var parentId = $("#parentId").val();
            var id = $("#id").val();
            $("#idZhu").html("");
            $("#ids").hide();
            $("#idZhu").hide();
            var reg = new RegExp("^[0-9][0-9]*$");

            var codeLen = 4;
            if (parentId == 0) {
                codeLen = 4;
            } else {
                codeLen = parentId.length + 4;
            }

            if (id == null || id.length < 1 || !reg.test(id)) {
                $("#ide").show();
                $("#idZhu").show();
                $("#idZhu").append("输入错误，请输入" + codeLen + "位数字编码！");
                if (focusFlag) {
                    locationFocus("id");
                }
                return false;
            } else if (id.length != codeLen) {
                $("#ids").show();
                $("#idZhu").show();
                $("#idZhu").append("输入错误，请输入" + codeLen + "位数字编码！");
                if (focusFlag) {
                    locationFocus("id");
                }
                return false;
            } else if (parentId > 0 && (id.substr(0, codeLen - 4)) != parentId) {
                $("#ids").show();
                $("#idZhu").show();
                $("#idZhu").append("输入错误，请输入以" + parentId + "开头的" + codeLen + "位数字编码！");
                if (focusFlag) {
                    locationFocus("id");
                }
                return false;
            } else {
                var flag = false;
                $.ajax({
                    url: "brand/validateBrandId",
                    type: "POST",
                    async: false,
                    data: {id: id},
                    success: function (data, status) {
                        //处理提示
                        var obj = eval(data);
                        if (data.key == 1) {
                            $("#ids").show();
                            $("#idZhu").show();
                            $("#idZhu").append("品牌ID已存在！");
                            if (focusFlag) {
                                locationFocus("id");
                            }
                        } else if (data.key == 2) {
                            $("#ids").show();
                            $("#idZhu").show();
                            $("#idZhu").append("后台系统错误，请稍后再试");
                            if (focusFlag) {
                                locationFocus("id");
                            }
                        } else {
                            flag = true;
                        }
                    }, error: function (data, status, e) {
                        $("#ids").show();
                        $("#idZhu").show();
                        $("#idZhu").append("后台系统错误，请稍后再试");
                        if (focusFlag) {
                            locationFocus("id");
                        }
                    }
                });
                return flag;

            }
        }


        function addSubmit() {
            if (!validateContentNotNull('brandName', 1, 30, '品牌名称', true)) {
                return false;
            }
            if (!brandIdBlur(true)) {
                return false;
            }

            $.ajax({
                type: "POST",
                url: "${pageContext.request.contextPath}/brand/saveBrand",
                dataType: "json",
                data: getParams(),
                success: function (data) {
                    if (data.errorCode == 0) {
                        prompt.alert("添加品牌信息成功！");

                        setTimeout(function () {
                            window.location.href = "${pageContext.request.contextPath}/brand/showBrand";
                        }, 2000);
                    } else {
                        prompt.alert(data.msg);
                    }
                }
            });
        }

        //获取form表单所有参数
        function getParams() {
            var json = {};
            $(".content input,.content select").each(function (i, e) {
                json[$(e).attr("id")] = $(e).attr("value");
            });
            return json;
        }

        // 数字必填验证
        function verify_Number(id,thisName) {
            var type = $("#type").val();
            if(type == 6) {
                var val = $("#" + id).val();
                $("#" + id + "Zhu").html("");
                $("#" + id + "s").hide();
                $("#" + id + "Zhu").hide();
                //开始验证
                if (verifyEmpty(val)) {
                    if (thisName != '' && thisName != null) {
                        $("#" + id + "Zhu").html(thisName + "值不能为空！");
                    } else {
                        $("#" + id + "Zhu").html("该值不能为空！");
                    }
                    $("#" + id + "Zhu").show();
                    return false;
                } else {
                    //纯数字正则
                    var numReg = new RegExp("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");

                    if (!numReg.test(val)) {
                        if (thisName != '' && thisName != null) {
                            $("#" + id + "Zhu").html(thisName + "由数字组成！");
                        } else {
                            $("#" + id + "Zhu").html("该值由数字组成！");
                        }
                        $("#" + id + "Zhu").show();
                        return false;
                    }
                    $("#" + id + "s").show();
                    return true;
                }
            }else{
                return true;
            }

        }

    </script>

</head>

<body id="body">
<!--head-->
<div id="head">
    <jsp:include page="../../common/head.jsp"></jsp:include>
</div>
<!--head end-->
<!--container-->
<div id="container">
    <div id="pageId" style="display: none;">05-02</div>
    <!--sidebar-->
    <%@ include file="../../common/menu.jsp" %>
    <!--sidebar-->
    <!--main-->
    <div class="main" id="mainDiv">
        <div class="channel_manage">
            <div class="location" style="background-position:22px -29px;">
                系统配置<span>&gt;</span>品牌管理<span>&gt;</span>添加品牌
            </div>
            <form method="post" enctype="multipart/form-data">
                <!--content-->
                <div class="content">
                    <div class="inputInfoList3">
                        <ul class="recharge_list" style="width:600;">
                            <li>
                                <label>上级编码品牌</label>编码：${parentId},品牌名：${parentBrandName}
                            </li>

                            <li>
                                <label>品牌编码</label>
                                <input type="text" id="id" name="id" value="${parentId}"
                                       onFocus="this.style.border = '1px solid #2A7CCF'"
                                       onblur="brandIdBlur();this.style.border = '1px solid #D5D5D5'" maxlength="24"/>
                                <span style="position:absolute;left:76%;top:9px;color:red;" class="red">*</span>
                                <i style="display: none;background-position: left center;" id="ids" class="s_tips"></i>
                                <i style="display: none;background-position: left center;padding-left:28%;" id="idZhu" class="zhu_shi_2"></i>
                            </li>
                            <li>
                                <label>品牌名称</label>
                                <input type="text" id="brandName" name="brandName"
                                       onFocus="this.style.border = '1px solid #2A7CCF'"
                                       onblur="validateContentNotNull('brandName',1,30,'品牌名称');this.style.border = '1px solid #D5D5D5'"/>
                                <span style="position:absolute;left:76%;top:9px;color:red;" class="red">*</span>
                                <i style="display: none;background-position: left center;" id="brandNames" class="s_tips"></i>
                                <i style="display: none;background-position: left center;padding-left:28%;" id="brandNameZhu" class="zhu_shi_2"></i>
                            </li>

                            <li style="position:relative;">
                                <input type="hidden" id="parentId" name="parentId" value="${parentId}"/>
                                <label>所属父品牌</label><input type="text" value="${parentBrandName}" readonly
                                                           onFocus="this.style.border = '1px solid #2A7CCF'"
                                                           onBlur="this.style.border = '1px solid #D5D5D5'">
                            </li>
                            <li>
                                <label>品牌类型</label>
                                <select id="type" name="type" style="width: 120px;">
                                    <option value="">选择品牌类型</option>
                                    <option value="1">汽车品牌</option>
                                    <option value="2">汽车厂商</option>
                                    <option value="3">汽车车系</option>
                                    <option value="4">汽车排量</option>
                                    <option value="5">汽车生产年份</option>
                                    <option value="6">汽车详细款</option>
                                </select>
                            </li>
                            <li>
                                <label>汽车最低价</label>
                                <input type="text" id="minPrice" name="minPrice"
                                       onFocus="this.style.border = '1px solid #2A7CCF'"
                                       onblur="verify_Number('minPrice','汽车最低价');this.style.border = '1px solid #D5D5D5'"
                                       maxlength="10"/>
                                <i style="display: none;background-position: left center;" id="minPrices" class="s_tips"></i>
                                <i style="display: none;background-position: left center;padding-left:28%;" id="minPriceZhu" class="zhu_shi_2"></i>
                            </li>
                            <li>
                                <label>汽车最高价</label>
                                <input type="text" id="maxPrice" name="maxPrice"
                                       onFocus="this.style.border = '1px solid #2A7CCF'"
                                       onblur="verify_Number('maxPrice','汽车最高价');this.style.border = '1px solid #D5D5D5'"
                                       maxlength="10"/>
                                <i style="display: none;background-position: left center;" id="maxPrices" class="s_tips"></i>
                                <i style="display: none;background-position: left center;padding-left:28%;" id="maxPriceZhu" class="zhu_shi_2"></i>
                            </li>
                            <li>
                                <label>汽车容量</label>
                                <input type="text" id="engineoilAmount" name="engineoilAmount"
                                       onFocus="this.style.border = '1px solid #2A7CCF'"
                                       onblur="verify_Number('engineoilAmount','汽车容量');this.style.border = '1px solid #D5D5D5'"
                                       maxlength="10"/>
                                <i style="display: none;background-position: left center;" id="engineoilAmounts" class="s_tips"></i>
                                <i style="display: none;background-position: left center;padding-left:28%;" id="engineoilAmountZhu" class="zhu_shi_2"></i>
                            </li>
                        </ul>
                        <div class="button_wrap">
                            <a href="javascript:void(0);" onClick="addSubmit();" class="bg_green">完成</a>
                            <input type="reset" value="重置">
                        </div>
                    </div>
                </div>
                <!--content-->
            </form>
            <!--foot-->
            <%@ include file="../../common/foot.jsp" %>
            <!--foot-->
        </div>
    </div>
    <!--main-->
</div>
<script src="${pageContext.request.contextPath}/static/js/basic.js"></script>

</body>
</html>
